package linkedhomework;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: ws
 * Date: 2023-01-05
 * Time: 21:42
 */
//https://www.nowcoder.com/practice/0e27e0b064de4eacac178676ef9c9d70?tpId=8&&tqId=11004&rp=2&ru=/activity/oj&qru=/ta/cracking-the-coding-interview/question-ranking
public class Partition {
    public ListNode1 partition(ListNode1 pHead, int x) {
        // write code here
        ListNode1 ls1 = new ListNode1(0);
        ListNode1 lb1 = ls1;
        ListNode1 ls2 = new ListNode1(0);
        ListNode1 lb2 = ls2;
        ListNode1 cur = pHead;
        while (cur != null) {
            if (cur.val < x) {
                lb1.next = cur;
                lb1 = cur;
            } else {
                lb2.next = cur;
                lb2 = cur;
            }
            cur = cur.next;
        }
        lb2.next = null;
        lb1.next = ls2.next;

        return ls1.next;
    }
}
class ListNode {
    int val;
    ListNode1 next = null;

    ListNode(int val) {
        this.val = val;
    }
}